﻿
using Caliburn.Micro;
using KouDai.WinPad.Helper;
using KouDai.WinPad.Models;
using KouDai.WinPad.Services;
using Newtonsoft.Json;
using NextEngine.Extens;
using NextEngine.HttpEngine;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Windows.ApplicationModel;

namespace KouDai.WinPad.ViewModels
{
    public class MainPageViewModel : KouDaiScreen
    {
        #region Fields
        private readonly INavigationService _navigationService;

        private ObservableCollection<HoneyEntity> _items = new ObservableCollection<HoneyEntity>();
        private long _minTimestamp = 0;
        private double _width = 240;
        private double _containerHeight = 530;
        private double _baseHeight = 265;
        private HoneyEntity _lastHoneyEntity;
        #endregion

        #region Constructor

        public MainPageViewModel()
        {
            if (DesignMode.DesignModeEnabled)
            {
                LoadDesignData();
            }
        }

        public MainPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            _navigationService = navigationService;
        }

        #endregion

        #region Properties

        public ObservableCollection<HoneyEntity> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.NotifyOfPropertyChange(() => this.Items);
                }
            }
        }

        public override bool Loading
        {
            get
            {
                return base.Loading;
            }
            set
            {
                if (value != base._loading)
                {
                    base._loading = value;
                    BusyInicator.Instance.IsBusy = value;
                    this.NotifyOfPropertyChange(() => this.Loading);
                }
            }
        }

        public string ID { get; set; }
        public string CurrentHotItemJson { get; set; }
        public string FatherHotItemJson { get; set; }
        public HotEntity CurrentHotItem { get; set; }
        public HotEntity FatherHotItem { get; set; }

        #endregion

        #region Override Method

        public override void LoadDesignData()
        {
            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15866793897_146x219.jpg",Price="100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao19765708787_146x194.jpg",Price="100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848113020_146x146.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848325228_146x219.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrlForIphone = "http://img.geilicdn.com/taobao19779004225_146x150.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865634518_146x229.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865434962_146x144.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15830113145_146x219.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrlForIphone = "http://img.geilicdn.com/taobao17077543430_146x167.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.35, ImageUrlForIphone = "http://img.geilicdn.com/taobao15857918167_146x197.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrlForIphone = "http://img.geilicdn.com/taobao19704356816_146x223.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao19725572516_146x194.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao19725916030_146x219.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.60, ImageUrlForIphone = "http://img.geilicdn.com/taobao15845742605_146x233.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.41, ImageUrlForIphone = "http://img.geilicdn.com/taobao15842817755_146x205.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15835866335_146x219.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrlForIphone = "http://img.geilicdn.com/taobao15693786979_146x223.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao17072171509_146x194.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.67, ImageUrlForIphone = "http://img.geilicdn.com/taobao17072151693_146x97.jpg", Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.51, ImageUrlForIphone = "http://img.geilicdn.com/taobao15882494639_146x220.jpg", Price = "100" });

            CaculateItems(honeyList);

            honeyList.ToObservableCollection(this.Items);
        }


        protected override void OnInitialize()
        {
            base.OnInitialize();
            LoadData();
            GetImageWall();
        }

        #endregion

        #region Method

        private void LoadData()
        {
            this.CurrentHotItem = JsonConvert.DeserializeObject<HotEntity>(CurrentHotItemJson);
            if (FatherHotItemJson != null)
            {
                this.FatherHotItem = JsonConvert.DeserializeObject<HotEntity>(FatherHotItemJson);
                this.DisplayName = CurrentHotItem.OriginalAppName + " ";
            }
            else
            {
                this.DisplayName = CurrentHotItem.OriginalAppName;
            }
        }

        public async void GetImageWall()
        {
            if (!Loading)
            {
                Loading = true;
                BackData backData = await ServiceManager.GetImageWall(ID, _minTimestamp);
                if (backData.IsSuccess)
                {
                    List<HoneyEntity> honeyList = backData.ResposeData as List<HoneyEntity>;
                    if (_lastHoneyEntity != null)
                    {
                        honeyList.Insert(0, _lastHoneyEntity);
                    }
                    Debug.WriteLine("Count------------>" + honeyList.Count);
                    CaculateItems(honeyList);
                    honeyList.ToObservableCollection(this.Items);
                    HoneyEntity lastEntity = honeyList[honeyList.Count - 1];
                    this._minTimestamp = lastEntity.Timestamp;
                }
                else
                {
                    Debug.WriteLine("msg:" + backData.Msg);
                }
                Loading = false;
            }

        }

        #endregion

        #region Util Method

        private void CaculateItems(List<HoneyEntity> honeyList)
        {
            int toSize = honeyList.Count;
            if (toSize % 2 == 1)
            {
                toSize -= 1;
                _lastHoneyEntity = honeyList[toSize];
                honeyList.Remove(_lastHoneyEntity);
            }
            else
            {
                _lastHoneyEntity = null;
            }
            for (int i = 0; i < toSize; i++)
            {
                if (i % 2 == 1)
                {
                    HoneyEntity lastItem = honeyList[i - 1];
                    HoneyEntity currentItem = honeyList[i];
                    double lastHeight = lastItem.ImgRatio * _width;
                    double currentHeight = currentItem.ImgRatio * _width;
                    lastItem.Width = _width;
                    lastItem.Height = _containerHeight * (lastHeight / (lastHeight + currentHeight));
                    lastItem.Margin = "0,0,0," + (_baseHeight - lastItem.Height).ToString();
                    //Debug.WriteLine("lastMargin :   " + lastItem.Margin);

                    currentItem.Width = _width;
                    currentItem.Height = _containerHeight - lastItem.Height;
                    currentItem.Margin = "0," + (_baseHeight - currentItem.Height).ToString() + ",0,0";
                    //Debug.WriteLine("currentMargin :   " + currentItem.Margin);
                }
            }
        }

        #endregion

        #region UIMethod

        public void SameLevelNavigate(string groupName)
        {
            HotEntity selectedItem = FatherHotItem.SubCateories.Where(x => x.OriginalAppName.Equals(groupName.Trim())).FirstOrDefault();
            if (selectedItem != null)
            {
                _navigationService.UriFor<MainPageViewModel>().
                WithParam<string>(x => x.DisplayName, selectedItem.OriginalAppName)
                .WithParam<string>(x => x.CurrentHotItemJson, JsonConvert.SerializeObject(selectedItem))
                .WithParam<string>(x => x.FatherHotItemJson, FatherHotItemJson)
                .WithParam<string>(x => x.ID, selectedItem.CombineAppId).Navigate();
                FrameStackHelper.Instance.AddCountStack();
            }
        }
        #endregion

    }
}
